package com.stations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stations.common.web.domain.request.PageDomain;
import com.stations.common.web.domain.response.Result;
import com.stations.domain.TTransmitter;
import com.stations.domain.TTransmitterProgram;
import com.stations.domain.TTransmitterTask;
import com.stations.domain.dtree.CheckArr;
import com.stations.domain.dtree.DTree;
import com.stations.mapper.TTransmitterMapper;
import com.stations.mapper.TTransmitterProgramMapper;
import com.stations.mapper.TTransmitterTaskMapper;
import com.stations.service.TrasmiterProgramService;
import com.stations.service.TrasmiterTaskService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.*;

/**
 * Describe: 用户服务实现类
 * Author: Ljy
 * CreateTime: 2019/10/23
 */
@Service
public class TrasmiterProgramServiceImpl extends ServiceImpl<TTransmitterProgramMapper, TTransmitterProgram> implements TrasmiterProgramService {

    @Resource
    private TTransmitterProgramMapper tTransmitterProgramMapper;

    @Resource
    private TTransmitterMapper tTransmitterMapper;
    /**
     * 递归，建立子树形结构
     * @return
     */
    @Override
    public List<DTree> getStaionTransmiterTree(){
        List<TTransmitter> lisTTransmitter = tTransmitterMapper.selectTTransmitter();
        return toStaionTTransmitterTree(lisTTransmitter);
    }
    /**
     * Describe: 递归获取菜单tree
     * Param: sysMenus
     * Return: 操作结果
     */
    public List<DTree> toStaionTTransmitterTree(List<TTransmitter> lisTTransmitter) {
        List<DTree> listDTree = new ArrayList<>();
        DTree dTreeP;
        DTree dTreeC;
        CheckArr checkArr =new CheckArr();
        List<CheckArr> checkArrs=new ArrayList<>();
        checkArr.setType("0");
        checkArr.setIsChecked("0");
        checkArrs.add(checkArr);
        HashMap<String,String> hash = new HashMap<>();
        for (TTransmitter tTransmitter : lisTTransmitter) {
            dTreeC = new DTree();
            dTreeC.setId(tTransmitter.getTransCode());
            dTreeC.setParentId(tTransmitter.getStationCode());
            dTreeC.setTitle(tTransmitter.getTransName());
            dTreeC.setCheckArr(checkArrs);
            dTreeC.setIconClass("-1");
            listDTree.add(dTreeC);
            dTreeP = new DTree();
            dTreeP.setId(tTransmitter.getStationCode());
            dTreeP.setParentId("0");
            dTreeP.setTitle(tTransmitter.getStationCode());
            dTreeP.setCheckArr(checkArrs);
            dTreeP.setIconClass("-1");
            if(!hash.containsKey(tTransmitter.getStationCode())){
                listDTree.add(dTreeP);
                hash.put(tTransmitter.getStationCode(),tTransmitter.getStationCode());
            }
        }
        List<DTree> treeDTrees =new  ArrayList<DTree>();
        for(DTree treeNode : getRootNode(listDTree)) {
            treeNode=buildChilTree(treeNode,listDTree);
            treeDTrees.add(treeNode);
        }
        return treeDTrees;
    }

    //递归，建立子树形结构
    public DTree buildChilTree(DTree pNode,List<DTree> listRegion){
        List<DTree> chilMenus =new  ArrayList<DTree>();
        for(DTree regionNode : listRegion) {
            if(regionNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(regionNode,listRegion));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点(获取所有的父节点)
    public List<DTree> getRootNode(List<DTree> listDTree) {
        List<DTree> rootRegionLists =new  ArrayList<DTree>();
        for(DTree regionNode : listDTree) {
            if(regionNode.getParentId().equals("0")) {
                rootRegionLists.add(regionNode);
            }
        }
        return rootRegionLists;
    }

    /**
     * 获取所有发射机节目表
     * @param
     * @return
     */
    @Override
    public List<TTransmitterProgram> selectList(){
        QueryWrapper<TTransmitterProgram> queryWrapper = new QueryWrapper<>();
        return tTransmitterProgramMapper.selectList(queryWrapper);
    }

    /**
     * 添加发射机节目
     * @param
     * @return
     */
    @Override
    public Result addProgram(TTransmitterProgram tTransmitterProgram) {
        tTransmitterProgram.setTaskCode(UUID.randomUUID().toString());
        tTransmitterProgram.setFlag("0");
        int insert = tTransmitterProgramMapper.insert(tTransmitterProgram);
        return insert ==1 ? Result.success("添加发射机节目成功!"):Result.failure("添加发射机节目失败!");
    }
    /**
     * 删除发射机节目
     * @param
     * @return
     */
    @Override
    public Result del(TTransmitterProgram tTransmitterProgram) {
        int i = tTransmitterProgramMapper.deleteById(tTransmitterProgram);
        return i == 1 ? Result.success("删除节目成功!"):Result.failure("删除节目失败!");
    }

    /**
     * 修改发射机节目
     * @param
     * @return
     */
    @Override
    public Result edit(TTransmitterProgram tTransmitterProgram) {
        int i = tTransmitterProgramMapper.updateById(tTransmitterProgram);
        return i == 1 ? Result.success("修改节目成功!"):Result.failure("修改节目失败!");
    }

    @Override
    public PageInfo<TTransmitterProgram> selectTTransmitterTaskPage(TTransmitterProgram tTransmitterProgram, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<TTransmitterProgram> data = tTransmitterProgramMapper.selectTTransmitterProgramList(tTransmitterProgram);
        return new PageInfo<>(data);
    }

    @Override
    public Result setsFileContent(MultipartFile file) {
        try {
            // 获取excel文件数据流
            InputStream is = file.getInputStream();
            // 创建工作簿
            Workbook wb = WorkbookFactory.create(is);
            Sheet sheet = wb.getSheetAt(0);
            TTransmitterProgram tTransmitterProgram;
            // 遍历每一行数据
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                tTransmitterProgram = new TTransmitterProgram();
                tTransmitterProgram.setStationCode(new DataFormatter().formatCellValue(row.getCell(0)));
                tTransmitterProgram.setTransCode(new DataFormatter().formatCellValue(row.getCell(1)));
                tTransmitterProgram.setTaskId(new DataFormatter().formatCellValue(row.getCell(2)));
                tTransmitterProgram.setSendDateTime(row.getCell(3).getDateCellValue());
                tTransmitterProgram.setSendDateEndTime(row.getCell(4).getDateCellValue());
                tTransmitterProgram.setProgramPeriod(new DataFormatter().formatCellValue(row.getCell(5)));
                tTransmitterProgram.setOperType(new DataFormatter().formatCellValue(row.getCell(6)));
                tTransmitterProgram.setChannelFreq(new DataFormatter().formatCellValue(row.getCell(7)));
                tTransmitterProgram.setChannelRfChState(new DataFormatter().formatCellValue(row.getCell(8)));
                tTransmitterProgram.setChannelTimeSlot(new DataFormatter().formatCellValue(row.getCell(9)));
                tTransmitterProgram.setChannelEqualization(new DataFormatter().formatCellValue(row.getCell(10)));
                tTransmitterProgram.setChannelRfChMod(new DataFormatter().formatCellValue(row.getCell(11)));
                tTransmitterProgram.setChannelRfChBand(new DataFormatter().formatCellValue(row.getCell(12)));
                tTransmitterProgram.setChannelRfChASource(new DataFormatter().formatCellValue(row.getCell(13)));
                tTransmitterProgram.setChannelRfChAUrl(new DataFormatter().formatCellValue(row.getCell(14)));
                tTransmitterProgram.setFlag("0");
                tTransmitterProgram.setSendTime(new Date());
                if(tTransmitterProgram.getStationCode()==null||tTransmitterProgram.getTransCode()==null){
                    continue;
                }
                tTransmitterProgram.setTaskCode(UUID.randomUUID().toString());
                tTransmitterProgramMapper.insert(tTransmitterProgram);
            }
        } catch (Exception e) {
            return Result.failure("运行图上传失败");
        }
        return Result.success("运行图上传成功");
    }

}
